package com.wnhz.work.jy.fragment.selectgoodpa;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.SelectGoodActivity;
import com.wnhz.work.jy.adapter.SelectGoodAdapter;
import com.wnhz.work.jy.bean.HomeFragment1.RecommendBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by d on 2016/10/17.
 */
public class Fragment4 extends Fragment implements View.OnClickListener {
    private static final String TAG = "Fragment4";
    private View view;

    private List<RecommendBean> list = new ArrayList<>();
    private SelectGoodAdapter adapter;
    private ListView listView;
    private SelectGoodActivity selectGoodActivity;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.select_good_listview, container, false);

        selectGoodActivity = (SelectGoodActivity) getActivity();
        return view;
    }

    public ArrayList<RecommendBean> getGoods(){
        ArrayList<RecommendBean> arrayList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getmYNum() > 0){
                arrayList.add(list.get(i));
            }
        }
        return arrayList;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        listView = (ListView) view.findViewById(R.id.listView);
        adapter = new SelectGoodAdapter(getActivity(),list,this);
        listView.setAdapter(adapter);
        getData("4");
    }

    @Override
    public void onClick(View view) {
        int i = (int) view.getTag();
        int j;
        switch (view.getId()){
            case R.id.add_img:
                j = list.get(i).getmYNum();
                if (j<Integer.parseInt(list.get(i).getStoreNum())){
                    j++;
                }else {
                    selectGoodActivity.MyToast("购买数量不可大于库存");
                }
                list.get(i).setmYNum(j);
                break;
            case R.id.reduce_img:
                j = list.get(i).getmYNum();
                if (j>0){
                    j--;
                    list.get(i).setmYNum(j);
                }
                break;
        }
        adapter.notifyDataSetChanged();
    }

    private void getData(String type){
        Map<String,Object> map = new HashMap<>();

        selectGoodActivity.showDialog();
        map.put("type",type);
        XUtil.Post(ConfigUtil.GOODS_LIST,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: "+result);
                try {
                    JSONArray array = new JSONArray(result);
                    Gson gson = new Gson();
                    list.clear();
                    for (int i = 0; i < array.length(); i++) {
                        RecommendBean bean = new RecommendBean();
                        bean = gson.fromJson(array.optString(i).toString(),RecommendBean.class);
                        String storeNum = bean.getStoreNum();
                        if (storeNum != null && !"".equals(storeNum) && Integer.parseInt(storeNum) > 0)
                            list.add(bean);
                    }
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                selectGoodActivity.closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                super.onCancelled(cex);
            }
        });
    }
}
